﻿using DnD.Model;
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace DnD.ViewModels
{
    class DeckViewModel : INotifyPropertyChanged
    {

        private Deck _selectedDeck;
        public ObservableCollection<Card> Cards { get; set; }
        private PhoneApplicationPage _view;
        private Card _selectedCard;

        public Deck SelectedDeck
        {
            get { return _selectedDeck; }
            set
            {
                _selectedDeck = value;
                OnPropertyChanged();

            }
        }

        public Card SelectedCard
        {
            get { return _selectedCard; }
            set
            {
                _selectedCard = value;
                OnPropertyChanged();
                showCard(value);
            }
        }


        public DeckViewModel(int deckID, PhoneApplicationPage view)
        {

            _view = view;
            Cards = new ObservableCollection<Card>();

            try
            {

                 var options = new DataLoadOptions();

                options.LoadWith<Deck>(d => d.Cards);
                options.LoadWith<CardDeck>(cd => cd.CardRef);

                using (var context = new DnDDataContext(DnDDataContext.DbConnectionString))
                {
                    context.LoadOptions = options;

                    var decks = from d in context.GameItems where d.Id == deckID select d;
                    //var decks = context.GameItems.OfType<Deck>().Where(d => d.Id == deckID);
                    foreach (Deck d in decks)
                    {
                        SelectedDeck = d;
                    }

                    //var cards = from cd in context.CardDecks from d in context.GameItems.OfType<Deck>() from c in context.GameItems.OfType<Card>() where c.Id == cd.CardRef.Id && d.Id == cd.DeckRef.Id && d.Id == deckID select c;
                    //var cards = from c in context.GameItems.OfType<Card>() select c;

                    

                    foreach (var card in SelectedDeck.Cards)
                    {
                        Cards.Add(card.CardRef);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }


        }

        public void showCard(Object card)
        {
            Card pressedCard = card as Card;


            _view.NavigationService.Navigate(new Uri("/CardPage.xaml?cardID=" + (pressedCard.Id).ToString(), UriKind.Relative));

        }

        public string BackGroundImageSource
        {
            get
            {
                if ((Visibility)App.Current.Resources["PhoneDarkThemeVisibility"]
                  == Visibility.Visible)
                {
                    return "/Assets/Pictures/fire.jpg";

                }
                else
                {
                    return "/Assets/Pictures/fire_negative.jpg";

                }
            }
            private set { }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
